<template>
  <div class="employee-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <div class="search-label">员工姓名：</div>
      <el-input
        clearable
        placeholder="请输入内容"
        class="search-main"
        v-model="params.name"
      />
      <el-button type="primary" @click="search">查询</el-button>
    </div>
    <div class="create-container">
      <el-button type="primary" @click="addUser">添加员工</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="employeeList">
        <el-table-column type="index" label="序号" :index="formatIndex()" />
        <el-table-column label="员工姓名" width="180" prop="name" />
        <el-table-column label="登录账号" width="180" prop="userName" />
        <el-table-column label="联系方式" width="120" prop="phonenumber" />
        <el-table-column label="角色" width="120" prop="roleName" />
        <el-table-column label="状态" width="120">
          <template #default="scope">
            {{ scope.row.status === 1 ? "启用" : "未启用" }}
          </template>
        </el-table-column>
        <el-table-column label="添加时间" prop="createTime" />
        <el-table-column label="操作" fixed="right" width="220">
          <template #default="scope">
            <el-button size="mini" type="text" @click="editUsers(scope.row)"
              >编辑</el-button
            >
            <el-button size="mini" type="text" @click="deleteUser(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-container">
      <el-pagination
        :total="total"
        :page-size="params.pageSize"
        @current-change="pageChange"
        layout="total, prev, pager, next"
      />
    </div>
    <!-- 添加员工 -->
    <el-dialog
      title="添加员工"
      width="480px"
      :visible="dialogVisible"
      @close="closeDialog"
      :close-on-click-modal="false"
    >
      <!-- 表单接口 -->
      <div class="form-container">
        <el-form
          ref="addForms"
          :model="addForm"
          :rules="addFormRules"
          label-width="80px"
        >
          <el-form-item label="员工姓名" prop="name">
            <el-input v-model="addForm.name" />
          </el-form-item>
          <el-form-item label="登录账号" prop="userName">
            <el-input v-model="addForm.userName" />
          </el-form-item>
          <el-form-item label="联系方式" prop="phonenumber">
            <el-input v-model="addForm.phonenumber" />
          </el-form-item>
          <el-form-item label="分配角色" prop="roleId">
            <el-select v-model="addForm.roleId" placeholder="请选择角色">
              <el-option
                v-for="item in roleList"
                :key="item.roleId"
                :label="item.roleName"
                :value="item.roleId"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="员工状态" prop="status">
            <el-radio-group v-model="addForm.status">
              <el-radio :label="0">未启用</el-radio>
              <el-radio :label="1">启用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <el-button size="mini" @click="closeDialog">取 消</el-button>
        <el-button size="mini" type="primary" @click="submitAddUser"
          >确 定</el-button
        >
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {
  getUserListAPI,
  reqGetRoleListAPI,
  addUserAPI,
  deleteUserAPI,
  getUserDetailAPI,
  updateUserAPI,
} from "@/api/system";
export default {
  name: "Employee",
  data() {
    return {
      employeeList: [],
      params: {
        page: 1,
        pageSize: 5,
        name: "",
      },
      total: 0,
      addForm: {
        name: "",
        userName: "",
        phonenumber: "",
        roleId: null,
        status: null,
      },
      addFormRules: {
        name: [{ required: true, message: "请填写员工姓名", trigger: "blur" }],
        userName: [
          { required: true, message: "请填写员工账号", trigger: "blur" },
        ],
        phonenumber: [
          { required: true, message: "请填写手机号码", trigger: "blur" },
        ],
        roleId: [
          { required: true, message: "请选择员工角色", trigger: "blur" },
        ],
        status: [
          { required: true, message: "请选择员工状态", trigger: "blur" },
        ],
      },
      dialogVisible: false,
      roleList: [],
    };
  },
  created() {
    this.getUserList();
  },
  methods: {
    async getUserList() {
      const res = await getUserListAPI(this.params);
      this.employeeList = res.data.rows;
      this.total = res.data.total;
    },
    pageChange(e) {
      this.params.page = e;
      this.getUserList();
    },
    search() {
      this.params.page = 1;
      this.getUserList();
    },
    formatIndex() {
      return (this.params.page - 1) * this.params.pageSize + 1;
    },
    async addUser() {
      this.dialogVisible = true;
      const res = await reqGetRoleListAPI();
      console.log(res, "角色");
      this.roleList = res.data;
    },
    submitAddUser() {
      this.$refs.addForms.validate(async (flag) => {
        if (!flag) {
          return;
        }
        if (!this.addForm.id) {
          await addUserAPI(this.addForm);
        } else {
          await updateUserAPI(this.addForm);
        }
        this.$message.success(this.addForm.id ? "修改成功" : "新增成功");
        // 关闭弹窗// 清空数据
        this.closeDialog();
        delete this.addForm.id;
        // 刷新列表
        this.getUserList();
      });
    },
    closeDialog() {
      this.dialogVisible = false;
      this.addForm = {
        name: "",
        userName: "",
        phonenumber: "",
        roleId: null,
        status: null,
      };
      delete this.addForm.id;
    },
    deleteUser(id) {
      this.$confirm("此操作将永久删除该用户，请三思而后行", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          console.log(id);
          const res = await deleteUserAPI(id);
          console.log(res);
          this.$message.success("删除成功");
          // 获取用户列表
          this.getUserList();
        })
        .catch(() => {});
    },
    async editUsers(rows) {
      const { data } = await getUserDetailAPI(rows.id);
      await this.addUser();
      this.addForm = {
        name: data.name,
        phonenumber: data.phonenumber,
        roleId: data.roleId,
        userName: data.userName,
        id: data.id,
        status: data.status,
      };
    },
  },
};
</script>

<style lang="scss" scoped>
.employee-container {
  padding: 10px;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, 0.9);
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.create-container {
  margin: 10px 0px;
}
.page-container {
  padding: 4px 0px;
  text-align: right;
}
.form-container {
  padding: 0px 80px;
}
</style>
